﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using JJY.CommonHelper;
using System.IO;
using mBook.Entity;
using mBook.DataAccess;

public partial class BookList_Upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string filePathAndName = Server.MapPath("~/TempFile/" + FileUpload1.FileName);
            FileUpload1.PostedFile.SaveAs(filePathAndName);

            DataTable dt = new ExcelHelper().GetDataTableFromExcel(filePathAndName);

            if (dt != null && dt.Rows.Count > 0)
            {
                List<BookList> list = new List<BookList>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(new BookList()
                    {
                        BookName = dt.Rows[i][0].ToString(),
                        Author = dt.Rows[i][1].ToString(),
                        BookID = dt.Rows[i][2].ToString(),
                        Introduce = dt.Rows[i][3].ToString()
                    });
                }

                new BookListDA().Insert(list);

                if (File.Exists(filePathAndName))
                {
                    File.Delete(filePathAndName);
                }
                lblInfo.Text = "Import successful.";
            }
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        new BookListDA().Remove();

        lblInfo.Text = "Clear successful.";
    }
}